package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.Comment;
import com.cjq.allservice.entity.User;
import com.cjq.allservice.entity.vo.CommentQuery;
import com.cjq.allservice.entity.vo.UserQuery;
import com.cjq.allservice.service.CommentService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-02-17
 */
@RestController
@RequestMapping("/allservice/comment")
@CrossOrigin
public class CommentController {

    private final CommentService commentService;

    public CommentController(CommentService commentService){
        this.commentService=commentService;
    }

    @ApiOperation("添加评论")
    @PostMapping("/addComment")
    public R addComment(@RequestBody Comment comment){
        boolean save = commentService.save(comment);
        if (save==true){
            return R.succ().message("添加评论成功！");
        }else {
            return R.fail().message("添加评论失败！");
        }
    }


    @ApiOperation("根据id获取评论信息")
    @GetMapping("/getCommentById")
    public R getCommentById(@RequestParam String commentId){
        Comment comment = commentService.getById(commentId);
        return R.succ().data("comment",comment);
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/deleteComment")
    public R deleteComment(@RequestParam String id){
        boolean b = commentService.removeById(id);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败！");
        }
    }

    @ApiOperation("后台管理员进行评论的回复")
    @PostMapping("/answerCommentById")
    public R answerCommentById(@RequestParam String commentId,@RequestParam String answer){
        Comment comment = commentService.getById(commentId);
        comment.setCommentAnswer(1);
        comment.setCommentContent(answer);
        boolean b = commentService.updateById(comment);
        if (b==true){
            return R.succ().message("回复成功！");
        }else {
            return R.fail().message("回复失败！");
        }
    }


    @ApiOperation("根据用户id获取评论")
    @GetMapping("/getCommentByUserId")
    public R getCommentByUserId(@RequestParam String userId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("comment_user_id",userId);
        List list = commentService.list(wrapper);
        return R.succ().data("comments",list);
    }


    @ApiOperation("根据商品id获取评论")
    @GetMapping("/getCommentByGoodsId")
    public R getCommentByGoodsId(@RequestParam String goodsId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("comment_goods_id",goodsId);
        List list = commentService.list(wrapper);
        return R.succ().data("comments",list);
    }

    @ApiOperation("获取所有的评论")
    @GetMapping("/getAllComment")
    public R getAllComment(){
        List list = commentService.list();
        return R.succ().data("comments",list);
    }

    @ApiOperation("条件查询用户带分页")
    //条件查询带分页
    @PostMapping ("pageCommentCondition/{current}/{limit}")
    public R pageCommentCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CommentQuery commentQuery){
        Page<Comment> page=new Page<>(current,limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String id = commentQuery.getCommentId();
        String name = commentQuery.getCommentInfo();
        String commentUserId=commentQuery.getCommentUserId();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("comment_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("comment_name",name);
        }
        if (!StringUtils.isEmpty(commentUserId)){
            wrapper.like("comment_user_id",commentUserId);
        }
        wrapper.orderByDesc("update_time");

        commentService.page(page,wrapper);
        long total=page.getTotal();
        System.out.println(total);
        List<Comment> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.succ().data(map);
    }



}

